Skip to content

PERF: ArrowExtensionArray.to_numpy #52525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 7, 2023
Merged

Conversation

lukemanley
Copy link
Member

There are a few different cases/paths in ArrowExtensionArray.to_numpy where copying can be avoided and more efficient fillna logic can be used:

import pandas as pd
import numpy as np

N = 10_000_000
data = np.random.randn(N)


# -----------------------------------------------------------------------------------
# case 1: no nulls

arr = pd.array(data, dtype="float64[pyarrow]")

%timeit arr.to_numpy("float64")

# 16 ms ± 620 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)        -> main
# 19.8 µs ± 711 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)  -> PR


# -----------------------------------------------------------------------------------
# case 2: contains nulls

arr = pd.array(data, dtype="float64[pyarrow]")
arr[::1000] = None

%timeit arr.to_numpy("float64", na_value=np.nan)

# 75.3 ms ± 3.48 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)  -> main
# 17 ms ± 204 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)    -> PR


# -----------------------------------------------------------------------------------
# case 3: null pyarrow type

arr = pd.array([None] * N, dtype="null[pyarrow]")

%timeit arr.to_numpy("float64", na_value=np.nan)

# 324 ms ± 14.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)     -> main
# 8.58 ms ± 214 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)  -> PR

@lukemanley lukemanley added Performance Memory or execution speed performance Arrow pyarrow functionality labels Apr 7, 2023
@mroeschke mroeschke added this to the 2.1 milestone Apr 7, 2023
@mroeschke mroeschke merged commit ce7522b into pandas-dev:main Apr 7, 2023
@mroeschke
Copy link
Member

Thanks @lukemanley

@lukemanley lukemanley deleted the arrow-numpy branch April 18, 2023 11:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Arrow pyarrow functionality Performance Memory or execution speed performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants